﻿
//时间日期的处理
(function ($, ibpm) {
    "use strict";
    $.extend(ibpm, {
        // 字串转化成日期
        parseDate: function (strDate) {
            var myDate;
            if (strDate.indexOf("/Date(") > -1)
                myDate = new Date(parseInt(strDate.replace("/Date(", "").replace(")/", ""), 10));
            else
                myDate = new Date(Date.parse(strDate.replace(/-/g, "/").replace("T", " ").split(".")[0]));//.split(".")[0] 用来处理出现毫秒的情况，截取掉.xxx，否则会出错
            return myDate;
        },
        // 日期格式化v日期,format:格式
        formatDate: function (v, format) {
            if (!v) return "";
            var d = v;
            if (typeof v === 'string') {
                if (v.indexOf("/Date(") > -1)
                    d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
                else
                    d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0])); //.split(".")[0] 用来处理出现毫秒的情况，截取掉.xxx，否则会出错
            }
            var o = {
                "M+": d.getMonth() + 1,  //month
                "d+": d.getDate(),       //day
                "h+": d.getHours(),      //hour
                "m+": d.getMinutes(),    //minute
                "s+": d.getSeconds(),    //second
                "q+": Math.floor((d.getMonth() + 3) / 3),  //quarter
                "S": d.getMilliseconds() //millisecond
            };
            if (/(y+)/.test(format)) {
                format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                }
            }
            return format;
        },
        // 获取当前时间;format为格式
        getDate: function (format, strInterval, Number) {
            var myDate = new Date();
            if (!!strInterval) {
                myDate = myDate.DateAdd(strInterval, Number);
            }
            var res = ibpm.formatDate(myDate, format);
            return res;
        },
        // 月
        getMonth: function () {
            var res = {
                begin: '',
                end: ''
            };
            var currentDate = ibpm.parseDate(ibpm.formatDate(new Date(), "yyyy-MM-01"));
            var endDate = currentDate.DateAdd('m', 1).DateAdd('d', -1);

            res.begin = ibpm.formatDate(currentDate, 'yyyy-MM-dd 00:00:00');
            res.end = ibpm.formatDate(endDate, 'yyyy-MM-dd 23:59:59');

            return res;
        },
        getPreMonth: function () {
            var res = {
                begin: '',
                end: ''
            };
            var currentDate = ibpm.parseDate(ibpm.formatDate(new Date(), "yyyy-MM-01"));
            var preMonth = currentDate.DateAdd('d', -1);

            res.begin = ibpm.formatDate(preMonth, 'yyyy-MM-01 00:00:00');
            res.end = ibpm.formatDate(preMonth, 'yyyy-MM-dd 23:59:59');

            return res;
        },
        // 季度
        getCurrentQuarter: function () {
            var currentDate = new Date();
            return ibpm.getQuarter(currentDate.getFullYear(), currentDate.getMonth());
        },
        getPreQuarter: function () {
            var currentDate = new Date().DateAdd('q', -1);
            return ibpm.getQuarter(currentDate.getFullYear(), currentDate.getMonth());
        },
        getQuarter: function (Year, month) {
            var res = {
                begin: '',
                end: ''
            };
            switch (month) {
                case 0:
                case 1:
                case 2:
                    res.begin = Year + "-01-01 00:00:00";
                    res.end = Year + "-03-31 23:59:59";
                    break;
                case 3:
                case 4:
                case 5:
                    res.begin = Year + "-04-01 00:00:00";
                    res.end = Year + "-06-30 23:59:59";
                    break;
                case 6:
                case 7:
                case 8:
                    res.begin = Year + "-07-01 00:00:00";
                    res.end = Year + "-09-30 23:59:59";
                    break;
                case 9:
                case 10:
                case 11:
                    res.begin = Year + "-10-01 00:00:00";
                    res.end = Year + "-12-31 23:59:59";
                    break;
            }
            return res;
        },
        // 年
        getYear: function () {
            var currentDate = new Date();
            var res = {
                begin: '',
                end: ''
            };
            var year = currentDate.getFullYear();
            res.begin = year + '-01-01 00:00:00';
            res.end = year + '-12-31 23:59:59';
            return res;
        },
        getPreYear: function () {
            var currentDate = new Date();
            var res = {
                begin: '',
                end: ''
            };
            var year = currentDate.getFullYear() - 1;
            res.begin = year + '-01-01 00:00:00';
            res.end = year + '-12-31 23:59:59';
            return res;
        },
        getFirstHalfYear: function () {
            var currentDate = new Date();
            var res = {
                begin: '',
                end: ''
            };
            var year = currentDate.getFullYear();
            res.begin = year + '-01-01 00:00:00';
            res.end = year + '-06-30 23:59:59';
            return res;
        },
        getSecondHalfYear: function () {
            var currentDate = new Date();
            var res = {
                begin: '',
                end: ''
            };
            var year = currentDate.getFullYear();
            res.begin = year + '-07-01 00:00:00';
            res.end = year + '-12-31 23:59:59';
            return res;
        }
    });
    //日期计算
    Date.prototype.DateAdd = function (strInterval, Number) {
        var dtTmp = this;
        switch (strInterval) {
            case 's': return new Date(Date.parse(dtTmp) + (1000 * Number));// 秒
            case 'n': return new Date(Date.parse(dtTmp) + (60000 * Number));// 分
            case 'h': return new Date(Date.parse(dtTmp) + (3600000 * Number));// 小时
            case 'd': return new Date(Date.parse(dtTmp) + (86400000 * Number));// 天
            case 'w': return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));// 星期
            case 'q': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());// 季度
            case 'm': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());// 月
            case 'y': return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());// 年
        }
    };
    //比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串
    Date.prototype.DateDiff = function (strInterval, dtEnd) {
        var dtStart = this;
        if (typeof dtEnd == 'string')   //如果是字符串转换为日期型  
        {
            dtEnd = ibpm.parseDate(dtEnd);
        }
        switch (strInterval) {
            case 's': return parseInt((dtEnd - dtStart) / 1000);
            case 'n': return parseInt((dtEnd - dtStart) / 60000);
            case 'h': return parseInt((dtEnd - dtStart) / 3600000);
            case 'd': return parseInt((dtEnd - dtStart) / 86400000);
            case 'w': return parseInt((dtEnd - dtStart) / (86400000 * 7));
            case 'm': return (dtEnd.getMonth() + 1) + ((dtEnd.getFullYear() - dtStart.getFullYear()) * 12) - (dtStart.getMonth() + 1);
            case 'y': return dtEnd.getFullYear() - dtStart.getFullYear();
        }
    };
    //取得当前日期所在月的最大天数
    Date.prototype.MaxDayOfDate = function () {
        var myDate = this;
        var ary = myDate.toArray();
        var date1 = (new Date(ary[0], ary[1] + 1, 1));
        var date2 = date1.DateAdd('m', 1);
        var result = dateDiff(date1.Format('yyyy-MM-dd'), date2.Format('yyyy-MM-dd'));
        return result;
    };
    //判断闰年
    Date.prototype.isLeapYear = function () {
        return (0 == this.getYear() % 4 && ((this.getYear() % 100 != 0) || (this.getYear() % 400 == 0)));
    };
    //---日期组件---
    $.fn.ibpm_datepicker = function (datedefault) {// 为input追加时间控件
        var $this = $(this);
        if (datedefault) {
            $this.val(datedefault);
        }
        $this.datepicker({
            todayBtn: "linked",
            language: "cn",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            autoclose: true
        });
    };
    $.fn.ibpm_datepickerTo = function () {  // 为input追加时间控件to
        var $this = $(this);
        $this.datepicker({
            keyboardNavigation: false,
            forceParse: false,
            autoclose: true,
            format: "yyyy-mm-dd"
        });
    };
})(jQuery, top.ibpm);

//数据验证完整性
(function ($, ibpm) {
    "use strict";
    // 数据验证方法
    ibpm.validator = {
        validReg: function (obj, reg, msg) {
            var res = { code: true, msg: '' };
            if (!reg.test(obj)) {
                res.code = false;
                res.msg = msg;
            }
            return res;
        },
        validRegOrNull: function (obj, reg, msg) {
            var res = { code: true, msg: '' };
            if (obj == null || obj == undefined || obj.length == 0) {
                return res;
            }
            if (!reg.test(obj)) {
                res.code = false;
                res.msg = msg;
            }
            return res;
        },
        isNotNull: function (obj) {// 验证不为空
            var res = { code: true, msg: '' };
            obj = $.trim(obj);
            if (obj == null || obj == undefined || obj.length == 0) {
                res.code = false;
                res.msg = '不能为空';
            }
            return res;
        },
        isNum: function (obj) {// 验证数字
            //return ibpm.validator.validReg(obj, /^[-+]?\d+$/, '必须为数字');
            return ibpm.validator.validReg(obj, /^\d+(.\d+)?$/, '必须为数字');
        },
        isNumOrNull: function (obj) {// 验证数字 或者空
            return ibpm.validator.validRegOrNull(obj, /^[-+]?\d+$/, '数字或空');
        },
        isEmail: function (obj) {//Email验证 email
            return ibpm.validator.validReg(obj, /^\w{3,}@\w+(\.\w+)+$/, '必须为E-mail格式');
        },
        isEmailOrNull: function (obj) {//Email验证 email   或者null,空
            return ibpm.validator.validRegOrNull(obj, /^\w{3,}@\w+(\.\w+)+$/, '必须为E-mail格式或空');
        },
        isEnglishStr: function (obj) {//验证只能输入英文字符串 echar
            return ibpm.validator.validReg(obj, /^[a-z,A-Z]+$/, '必须为英文字符串');
        },
        isEnglishStrOrNull: function (obj) {//验证只能输入英文字符串 echar 或者null,空
            return ibpm.validator.validRegOrNull(obj, /^[a-z,A-Z]+$/, '必须为英文字符串或空');
        },
        isTelephone: function (obj) { //验证是否电话号码 phone
            return ibpm.validator.validReg(obj, /^(\d{3,4}\-)?[1-9]\d{6,7}$/, '必须为电话格式');
        },
        isTelephoneOrNull: function (obj) {//验证是否电话号码 phone或者null,空
            return ibpm.validator.validRegOrNull(obj, /^(\d{3,4}\-)?[1-9]\d{6,7}$/, '必须为电话格式或空');
        },
        isMobile: function (obj) {//验证是否手机号 mobile
            return ibpm.validator.validReg(obj, /^(\+\d{2,3}\-)?\d{11}$/, '必须为手机格式');
        },
        isMobileOrnull: function (obj) {//验证是否手机号 mobile或者null,空
            return ibpm.validator.validRegOrNull(obj, /^(\+\d{2,3}\-)?\d{11}$/, '必须为手机格式或空');
        },
        isMobileOrPhone: function (obj) {//验证是否手机号或电话号码 mobile phone 
            var res = { code: true, msg: '' };
            if (!ibpm.validator.isTelephone(obj).code && !ibpm.validator.isMobile(obj).code) {
                res.code = false;
                res.msg = '为电话格式或手机格式';
            }
            return res;
        },
        isMobileOrPhoneOrNull: function (obj) {//验证是否手机号或电话号码 mobile phone或者null,空
            var res = { code: true, msg: '' };
            if (ibpm.validator.isNotNull(obj).code && !ibpm.validator.isTelephone(obj).code && !ibpm.validator.isMobile(obj).code) {
                res.code = false;
                res.msg = '为电话格式或手机格式或空';
            }
            return res;
        },
        isUri: function (obj) {//验证网址 uri
            return ibpm.validator.validReg(obj, /^http:\/\/[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/, '必须为网址格式');
        },
        isUriOrNull: function (obj) {//验证网址 uri或者null,空
            return ibpm.validator.validRegOrNull(obj, /^http:\/\/[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/, '必须为网址格式或空');
        },
        isDate: function (obj) {//判断日期类型是否为YYYY-MM-DD格式的类型 date
            return ibpm.validator.validReg(obj, /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/, '必须为日期格式');
        },
        isDateOrNull: function (obj) {//判断日期类型是否为YYYY-MM-DD格式的类型 date或者null,空
            return ibpm.validator.validRegOrNull(obj, /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/, '必须为日期格式或空');
        },
        isDateTime: function (obj) {//判断日期类型是否为YYYY-MM-DD hh:mm:ss格式的类型 datetime
            return ibpm.validator.validReg(obj, /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/, '必须为日期时间格式');
        },
        isDateTimeOrNull: function (obj) {//判断日期类型是否为YYYY-MM-DD hh:mm:ss格式的类型 datetime或者null,空
            return ibpm.validator.validRegOrNull(obj, /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/, '必须为日期时间格式');
        },
        isTime: function (obj) {//判断日期类型是否为hh:mm:ss格式的类型 time
            return ibpm.validator.validReg(obj, /^((20|21|22|23|[0-1]\d)\:[0-5][0-9])(\:[0-5][0-9])?$/, '必须为时间格式');
        },
        isTimeOrNull: function (obj) {//判断日期类型是否为hh:mm:ss格式的类型 time或者null,空
            return ibpm.validator.validRegOrNull(obj, /^((20|21|22|23|[0-1]\d)\:[0-5][0-9])(\:[0-5][0-9])?$/, '必须为时间格式或空');
        },
        isChinese: function (obj) {//判断输入的字符是否为中文 cchar 
            return ibpm.validator.validReg(obj, /^[\u0391-\uFFE5]+$/, '必须为中文');
        },
        isChineseOrNull: function (obj) {//判断输入的字符是否为中文 cchar或者null,空
            return ibpm.validator.validRegOrNull(obj, /^[\u0391-\uFFE5]+$/, '必须为中文或空');
        },
        isZip: function (obj) {//判断输入的邮编(只能为六位)是否正确 zip
            return ibpm.validator.validReg(obj, /^\d{6}$/, '必须为邮编格式');
        },
        isZipOrNull: function (obj) {//判断输入的邮编(只能为六位)是否正确 zip或者null,空
            return ibpm.validator.validRegOrNull(obj, /^\d{6}$/, '必须为邮编格式或空');
        },
        isDouble: function (obj) {//判断输入的字符是否为双精度 double
            return ibpm.validator.validReg(obj, /^[-\+]?\d+(\.\d+)?$/, '必须为小数');
        },
        isDoubleOrNull: function (obj) {//判断输入的字符是否为双精度 double或者null,空
            return ibpm.validator.validRegOrNull(obj, /^[-\+]?\d+(\.\d+)?$/, '必须为小数或空');
        },
        isIDCard: function (obj) {//判断是否为身份证 idcard
            return ibpm.validator.validReg(obj, /^\d{15}(\d{2}[A-Za-z0-9;])?$/, '必须为身份证格式');
        },
        isIDCardOrNull: function (obj) {//判断是否为身份证 idcard或者null,空
            return ibpm.validator.validRegOrNull(obj, /^\d{15}(\d{2}[A-Za-z0-9;])?$/, '必须为身份证格式或空');
        },
        isIP: function (obj) {//判断是否为IP地址格式
            var res = { code: true, msg: '' };
            var reg = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正则表达式 
            var flag = false;
            if (reg.test(obj)) {
                if (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256) { flag = true };
            }
            if (!flag) {
                res.code = false;
                res.msg = '必须为IP格式';
            }
            return res;
        },
        isIPOrNull: function (obj) {//判断是否为IP地址格式 或者null,空
            var res = { code: true, msg: '' };
            if (ibpm.validator.isNotNull(obj) && !ibpm.validator.isIP(obj).code) {
                res.code = false;
                res.msg = '必须为IP格式或空';
            }
            return res;
        },
        isLenNum: function (obj, n) {//验证是否是n位数字字符串编号 nnum
            var res = { code: true, msg: '' };
            var reg = /^[0-9]+$/;
            obj = $.trim(obj);
            if (obj.length > n || !reg.test(obj)) {
                res.code = false;
                res.msg = '必须为' + n + '位数字';
            }
            return res;
        },
        isLenNumOrNull: function (obj, n) {//验证是否是n位数字字符串编号 nnum或者null,空
            var res = { code: true, msg: '' };
            if (ibpm.validator.isNotNull(obj).code && !ibpm.validator.isLenNum(obj)) {
                res.code = false;
                res.msg = '必须为' + n + '位数字或空';
            }
            return res;
        },
        isLenStr: function (obj, n) {//验证是否小于等于n位数的字符串 nchar
            var res = { code: true, msg: '' };
            obj = $.trim(obj);
            if (!ibpm.validator.isNotNull(obj).code || obj.length > n) {
                res.code = false;
                res.msg = '必须小于等于' + n + '位字符';
            }
            return res;
        },
        isLenStrOrNull: function (obj, n) {//验证是否小于等于n位数的字符串 nchar或者null,空
            var res = { code: true, msg: '' };
            obj = $.trim(obj);
            if (ibpm.validator.isNotNull(obj).code && obj.length > n) {
                res.code = false;
                res.msg = '必须小于等于' + n + '位字符或空';
            }
            return res;
        }
    };
})(window.jQuery, top.ibpm);

function toDecimal(val) {
    var n = parseFloat(val);
    return isNaN(n) ? val : n
}